package mobi.hami.huapin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import mobi.hami.huapin.base.Result;
import mobi.hami.huapin.constants.BaseEnums;
import mobi.hami.huapin.dto.*;
import mobi.hami.huapin.model.*;
import mobi.hami.huapin.service.*;
import mobi.hami.huapin.util.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping(value = "shot", produces = "application/json")
@Api("商品属性")
public class SkuAttributeController {
    @Autowired
    private SkuPictureMappingService sku_picture_mapping;
    @Autowired
    private SkuAffiliatedinfoService skuAffiliatedinfoService;
    @Autowired
    private SkuProductattributeMappingService productattributeMappingService;
    @Autowired
    private SkuAttributeService skuAttributeService;
    @Autowired
    private SkuAttributeValueService skuAttributeValueService;
    @Autowired
     private  ProductShowAttributeService productShowAttributeService;
    @RequestMapping(value = "/add/attribute.do", method = RequestMethod.POST)
    @ApiOperation(value = "添加属性", notes = " ")
    public Result addAttribute(@RequestBody SkuAttribute skuAttribute) {
        return Results.successWithData(skuAttributeService.add(skuAttribute), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
    @RequestMapping(value = "/update/attribute.do", method = RequestMethod.POST)
    @ApiOperation(value = "修改属性名", notes = " ")
    public Result updateAttribute(@RequestBody SkuAttribute skuAttribute) {
        Date da=new Date();
        skuAttribute.setGmt_modified(da);
        skuAttribute.setGmt_create(da);
        return Results.successWithData(skuAttributeService.Update(skuAttribute), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
    @RequestMapping(value = "/add/attributeValue.do", method = RequestMethod.POST)
    @ApiOperation(value = "添加属性值", notes = " ")
    public Result addAttributeValue(@RequestBody SkuAttributevalue skuAttributevalue) {
        return Results.successWithData(skuAttributeValueService.add(skuAttributevalue), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
    @RequestMapping(value = "/update/attributeValue.do", method = RequestMethod.POST)
    @ApiOperation(value = "修改属性值", notes = " ")
    public Result updateAttributeValue(@RequestBody SkuAttributevalue skuAttributevalue) {
        return Results.successWithData(skuAttributeValueService.Update(skuAttributevalue), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/productSkuList.do", method = RequestMethod.POST)
    @ApiOperation(value = "查询商品下的Sku", notes = " ")
    public Result addAttribute(@RequestBody AttributeDTO attributeDTO) {
        Date date = new Date();
        //保存图片
        if (attributeDTO.pic_attach_url.size() > 0) {
            List<SkuPictureMapping> list = new ArrayList<SkuPictureMapping>();
            for (ImgDTO imgDTO : attributeDTO.pic_attach_url) {
                if (imgDTO.isdelete) {
                    SkuPictureMapping model = new SkuPictureMapping();
                    model.setDeleted(0);
                    model.setGmt_create(date);
                    model.setDisplayorder(0);
                    model.setGmt_modified(date);
                    model.setMastertag(imgDTO.type);
                    model.setPictuire(imgDTO.getImg());
                    model.setSpuid(attributeDTO.getProductid());
                    model.setSource(0);
                    sku_picture_mapping.add(model);
                }
            }
        }
        //存在skushu属性就进行修改
        SkuAffiliatedinfo skuinfo = skuAffiliatedinfoService.isInfo(attributeDTO.getSkuid());
        SkuAffiliatedinfo info = new SkuAffiliatedinfo();
        info.setSkuid(attributeDTO.getSkuid());
        info.setCartonunit(attributeDTO.getCartonunit());
        info.setCartonvalue(attributeDTO.getCartonvalue());
        info.setDeleted(attributeDTO.getDeleted());
        info.setDescription(attributeDTO.getDescription());
        info.setFragile(attributeDTO.getFragile());
        info.setGmt_create(date);
        info.setGmt_modified(date);
        info.setUnit(attributeDTO.getUnit());
        info.setVolume(attributeDTO.getVolume());
        info.setWeight(attributeDTO.getWeight());
        if (skuinfo != null && skuinfo.getId() > 0) {
            info.setId(skuinfo.getId());
            skuAffiliatedinfoService.Update(info);
        } else {
            ///保存商品属性
            skuAffiliatedinfoService.add(info);
        }
        if (attributeDTO.sku_productattribute_mapping.size() > 0) {
            for (ProductattributeDTO productattributeDTO : attributeDTO.sku_productattribute_mapping) {
                if (productattributeDTO.getProductattributeid() > 0) {
                    SkuProductattributeMapping mapping = new SkuProductattributeMapping();
                    mapping.setSkuid(attributeDTO.getSkuid());
                    mapping.setGmt_create(date);
                    mapping.setGmt_modified(date);
                    mapping.setProductattributeid(productattributeDTO.getProductattributeid());
                    mapping.setProductattributevalueid(productattributeDTO.getProductattributevalueid());
                    productattributeMappingService.Add(mapping);
                }
            }


        }
        if(attributeDTO.productShowAttribute.size()>0){
            productShowAttributeService.insertList(attributeDTO.productShowAttribute);
        }
        return Results.successWithData(1, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getAttributeByProductid.do", method = RequestMethod.GET)
    @ApiOperation(value = "根据商品id查询商品属性", notes = " ")
    public Result getAttributeByProductid(Integer productid) {
        return Results.successWithData(skuAttributeService.getAttributeByProductid(productid), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
    @RequestMapping(value = "/delte/id.do", method = RequestMethod.POST)
    @ApiOperation(value = "根据属性名id删除属性名", notes = " ")
    public Result delteAttributeId(@RequestBody SkuAttribute skuAttribute) {
        return Results.successWithData(skuAttributeService.delteAttributeIds(skuAttribute.getId()), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/delte/value/id.do", method = RequestMethod.POST)
    @ApiOperation(value = "根据属性值id删除属性值", notes = " ")
    public Result delteAttributeValueId(@RequestBody SkuAttributevalue skuAttribute) {
        return Results.successWithData(skuAttributeValueService.delteAttributeValueId(skuAttribute.getId()), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getattribute/byvalueid.do", method = RequestMethod.GET)
    @ApiOperation(value = "根据属性名id查询商品属性值", notes = " ")
    public Result getattribute_byvalueid(Integer attribut) {
        return Results.successWithData(skuAttributeValueService.getattribute_byvalueid(attribut), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/getAttributeValueByProductid.do", method = RequestMethod.GET)
    @ApiOperation(value = "根据商品id查询商品属性值", notes = " ")
    public Result getAttributeValueByProductid(Integer productid) {
        return Results.successWithData(skuAttributeValueService.getAttributeVslueByProductid(productid), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/attribute/list.do", method = RequestMethod.POST)
    @ApiOperation(value = "获取分页属性", notes = "")
    public Result getList(@RequestBody PageDTO page) {
        page.setData(new ArrayList<Product>());
        page.setPageindex((page.getPageindex() - 1) * page.getPagesize() >= 0 ? (page.getPageindex() - 1) * page.getPagesize() : 0);
        page.setPagesize(page.getPagesize() > 0 ? page.getPagesize() : Integer.MAX_VALUE);
        PageDTO<SkuAttribute> products = skuAttributeService.getListPage(page);
        return Results.successWithData(products, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/select/skuimg.do", method = RequestMethod.GET)
    @ApiOperation(value = "查询spu下未删除得图片", notes = "")
    public Result selectImg(Integer spuid) {
        return Results.successWithData(sku_picture_mapping.selectImg(spuid), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/select/selectAttribute_mapping.do", method = RequestMethod.GET)
    @ApiOperation(value = "查询sku下", notes = "")
    public Result selectAttribute_mapping(Integer skuid) {
        return Results.successWithData(productattributeMappingService.selectAttribute_mapping(skuid), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/select/affiliatedinfo.do", method = RequestMethod.GET)
    @ApiOperation(value = "查询sku下商品形态属性", notes = "")
    public Result selectAffiliatedinfo(Integer skuid) {
        return Results.successWithData(skuAffiliatedinfoService.selectAffiliatedinfo(skuid), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
    @RequestMapping(value = "/select/productall.do", method = RequestMethod.GET)
    @ApiOperation(value = "查询spu下商品显示属性", notes = "")
    public Result selectProductAll(Integer spuid) {
        return Results.successWithData(  productShowAttributeService.selectProductAll(spuid), BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }

    @RequestMapping(value = "/update/affiliated.do", method = RequestMethod.POST)
    @ApiOperation(value = "修改商品属性", notes = "")
    public Result updateAttributed(@RequestBody AttributeDTO attributeDTO) {
        Date date = new Date();


            List<SkuPictureMapping> list = new ArrayList<SkuPictureMapping>();
            for (ImgDTO imgDTO : attributeDTO.pic_attach_url) {
                if (imgDTO.isdelete) {
                    SkuPictureMapping model = new SkuPictureMapping();
                    model.setDisplayorder(0);
                    model.setGmt_modified(date);
                    model.setMastertag(imgDTO.type);
                    model.setPictuire(imgDTO.getImg());
                    model.setSpuid(attributeDTO.getProductid());
                    model.setDisplayorder(0);
                    model.setSource(0);
                    model.setGmt_create(date);
                    if(imgDTO.getId()>0){
                        model.setDeleted(0);
                        model.setId(imgDTO.getId());
                        sku_picture_mapping.update(model);
                    }else {
                        model.setDeleted(0);
                        sku_picture_mapping.add(model);
                    }
                } else {
                    sku_picture_mapping.falseDelete(imgDTO.getId());
                }
            }
        //存在skushu属性就进行修改
        SkuAffiliatedinfo skuinfo = skuAffiliatedinfoService.isInfo(attributeDTO.getSkuid());
        SkuAffiliatedinfo info = new SkuAffiliatedinfo();
        info.setSkuid(attributeDTO.getSkuid());
        info.setCartonunit(attributeDTO.getCartonunit());
        info.setCartonvalue(attributeDTO.getCartonvalue());
        info.setDeleted(attributeDTO.getDeleted());
        info.setDescription(attributeDTO.getDescription());
        info.setFragile(attributeDTO.getFragile());
        info.setGmt_create(date);
        info.setGmt_modified(date);
        info.setUnit(attributeDTO.getUnit());
        info.setVolume(attributeDTO.getVolume());
        info.setWeight(attributeDTO.getWeight());
        if (skuinfo != null && skuinfo.getId() > 0) {
            info.setId(skuinfo.getId());
            skuAffiliatedinfoService.Update(info);
        } else {
            ///保存商品属性
            skuAffiliatedinfoService.add(info);
        }
            productattributeMappingService.DeleteAll(attributeDTO.getSkuid());
            for (ProductattributeDTO productattributeDTO : attributeDTO.sku_productattribute_mapping)
            {
                SkuProductattributeMapping mapping = new SkuProductattributeMapping();
                mapping.setSkuid(attributeDTO.getSkuid());
                mapping.setGmt_modified(date);
                mapping.setProductattributeid(productattributeDTO.getProductattributeid());
                mapping.setProductattributevalueid(productattributeDTO.getProductattributevalueid());
                if (productattributeDTO.getProductattributeid() > 0) {
                    mapping.setGmt_create(date);
                    productattributeMappingService.Add(mapping);
                }
            }
            productShowAttributeService.deleteAll(attributeDTO.getProductid());
            productShowAttributeService.insertList(attributeDTO.productShowAttribute);

        return Results.successWithData(1, BaseEnums.SUCCESS.code(), BaseEnums.SUCCESS.desc());
    }
}
